/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hu.rsc.feeds.client.gui;

import hu.rsc.feeds.client.data.Entry;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;

/**
 * 
 * @author arbuckle
 */
public class PostWidget extends Composite {

    Label label;
    private boolean blink = false;

    Timer timer = new Timer() {
	@Override
	public void run() {
	    if (PostWidget.this.blink) {
		PostWidget.this.label.addStyleName("blink");
	    } else {
		PostWidget.this.label.removeStyleName("blink");
	    }
	    PostWidget.this.blink = !PostWidget.this.blink;
	}
    };

    public PostWidget(final Entry entry, final String title) {
	this.label = new Label();
	this.label.setStyleName("entry");
	initWidget(this.label);
	this.addStyleName("border");
	this.label.setText(entry.getTitle());
	this.label.addClickHandler(new ClickHandler() {

	    @Override
	    public void onClick(ClickEvent event) {
		stopBlinking();
		final PostPopup popup = new PostPopup(entry, title);
		final int x = event.getNativeEvent().getClientX();
		final int y = event.getNativeEvent().getClientY();
		popup.setPopupPositionAndShow(new PopupPanel.PositionCallback() {

		    @Override
		    public void setPosition(int offsetWidth, int offsetHeight) {
			int left = x;
			int top = y;
			popup.setPopupPosition(left, top);
		    }
		});

	    }
	});
    }

    public void startBlinking() {
	this.timer.scheduleRepeating(500);
    }

    public void stopBlinking() {
	this.timer.cancel();
	this.label.removeStyleName("blink");
    }
}
